/*
 * @Description:
 * @Version: 2.0
 * @Autor: wsh
 * @Date: 2023-06-20 16:06:20
 * @LastEditors: uphldjx 1373577124@qq.com
 * @LastEditTime: 2024-04-23 17:30:13
 */
import { uploadType,regularRule,formateDate ,getAreaObj,getBaseAreaData } from '@/utils/util'
import { getStore } from '@/utils/store'
import { cloneDeep } from 'lodash-es'
import apiMod from '@/apiMod/index'
import { IdCard } from '@icon-park/vue-next'

let currentYear = String(new Date().getFullYear())
let isUploadDic = [
	{label:"全部",value:"",type:"primary"},
	{label:"未上传",value:"0",type:"danger"},
	{label:"已上传",value:"1",type:"success"}
]

// 列表
const tableConfig = {
	stripe: true,
	border: true,
	maxHeight: '80vh',
	columns: [
        { label: '归属年份', prop: 'belongYear' },
		{ label: '老人姓名', prop: 'name' },
		{ label: '身份证号', prop: 'idCard',plusXing: '2,2' },
		{
			label: '手机号码',
			prop: 'telephone',
            plusXing: '2,2'
		},
		{
			label: '户籍区域',
			prop: 'cr_areas_name',
		},
		{ label: '现居区域', prop: 'areas_name' },
		{
			label: '申请时间',
			prop: 'applicationTime',
		},
		{
			label: '改造申请表上传情况',
			prop: 'isWithMaterial',
			custome:"tag",
			dicData:isUploadDic
		},
		{
			label: '改造申请表',
			prop: 'applicationFile1',
		},
		{
			label: '改造申请表申请时间',
			prop: 'materialUploadTime',
		},
		{
			label: '操作',
			prop: 'menu',
			width: 300,
			fixed: 'right',
		},
	],
}

const formConfig = {
	stripe: true,
	border: true,
    inline:false,
    // labelWidth:'200px',
    labelPostion:'left',
	maxHeight: '80vh',
	column:[
        {
            label:"适老化改造项目归属年份",
			prop:"belongYear",
            type:"year",
            format: 'YYYY年',
            valueFormat: 'YYYY',
			defaultValue:currentYear,
            span:24,
            rules:[
                {required:true,message:"请填写适老化改造项目归属年份"}
            ]
        },
		{
			label:"身份证号",
			prop:"idCard",
			change:async(item, form, value)=>{
				let params = {
					businessType: 4,
					processStatus:1,
					isWithAuth:0, // 0否1是
					idCard:value
				}
				let res = await apiMod.view_bscBusinessProcess_page(params)
				if(res.data.records&&res.data.records.length>0){
					let data = res.data.records[0]
					form.name = data.oldmanName
					// form.idCard=data.idCard
					form.telephone=data.oldmanPhone
					let cr_areas:any = getAreaObj(data, 'cr','set')
					let areas:any = getAreaObj(data, '','set')
					form.cr_areas = cr_areas.areaIds
					form.cr_areas_name = cr_areas.names
					form.areas = areas.areaIds
					form.areas_name = areas.names
					form.crAddress = data.crAddress
					form.address = data.address
				}else{
					form.name = ''
					// form.idCard=data.idCard
					form.telephone=''
					form.cr_areas = []
					form.cr_areas_name = ''
					form.areas = []
					form.areas_name = ''
					form.crAddress = ''
					form.address = ''
				}
				
			},
			rules:[
				{required:true,pattern:regularRule.idCard,message:"请填写身份证号"}
			],

			span:24
		},
		{
			label:"老人姓名",
			prop:"name",
			// propName:"name",
            // type: 'autoSelect',
			// api: 'view_bscBusinessProcess_page',
			// filterKey: 'oldmanName',
			// filterLabel: 'oldmanName',
			// multiple: false,
            // paramsObj: {
			// 	businessType: 4,
			// 	processStatus:1
			// 	// roleName: '安全员',
			// },
			// change:(item, form, value)=>{
			// 	form.idCard=item.idCard
			// 	form.telephone=item.oldmanPhone
			// 	let cr_areas:any = getAreaObj(item, 'cr','set')
			// 	let areas:any = getAreaObj(item, '','set')
			// 	form.cr_areas = cr_areas.areaIds
			// 	form.cr_areas_name = cr_areas.names
			// 	form.areas = areas.areaIds
			// 	form.areas_name = areas.names
			// 	form.crAddress = item.crAddress
			// 	form.address = item.address
			// },
			span:24,
			rules:[
				{required:true,message:"请填写老人姓名"}
			]
		},
		
        {
			label:"手机号码",
			prop:"telephone",
			span:24,
			rules:[
				{required:true,pattern:regularRule.phone,message:"请填写联系电话"}
			]
		},
        {
			label:"户籍区域",
            type:"AplidAreaSelect",
			prop:"cr_areas",
			checkStrictly:false,
            propName:"cr_areas_name",
			span:24,
			rules:[
				{required:true,message:"请填写户籍区域"}
			]
		},
        {
			label:"户籍地址",
			prop:"crAddress",
			span:24,
			rules:[
				{required:true,message:"请填写户籍地址"}
			]
		},
        {
			label:"现居区域",
            type:"AplidAreaSelect",
			prop:"areas",
			checkStrictly:false,
            propName:"areas_name",
			span:24,
			rules:[
				{required:true,message:"请填写现居区域"}
			]
		},
        {
			label:"现居地址",
			prop:"address",
			span:24,
			rules:[
				{required:true,message:"请填写现居地址"}
			]
		},
        {
			label:"申请时间",
            type:'date',
            format: 'YYYY年MM月DD',
            valueFormat: 'YYYY-MM-DD',
			prop:"applicationTime",
			span:24,
			rules:[
				{required:true,message:"请填写申请时间"}
			]
		},
        {
            label:"适老化改造申请表",
            type:'upload',
			prop:"applicationFile",
			listType: 'picture-card',
			custome:"upload",
			fileType:'application/pdf,image/png,image/jpg,image/jpeg',
            accept:'.pdf,.png,.jpg,.jpeg',
			labelPosition: 'top',
			// apiName: 'ost_uploadFile2',
			showFileList: true,
			multiple: true,
			uploadTip:"上传文件（pdf、图片）",
			span:24,
			change:(item, form, value)=>{
				if(value){
					form.materialUploadTime = formateDate(new Date(),true)
				}else{
					form.materialUploadTime = ''
				}
				
			},
			rules:[
				{required:true,message:"请上传适老化改造申请表"}
			]
        }
	]
}
export const applyFormConfig = formConfig
export const api = {
	index: 'view_bscAdaptOldApply_page',
	add: 'view_bscAdaptOldApply_add',
	detail:"view_bscAdaptOldApply_view",
	edit: 'view_bscAdaptOldApply_edit',
	delete: 'view_bscAdaptOldApply_del',
	export:'view_bscAdaptOldApply_export',
	import:"view_bscAdaptOld_apply_import"
}

export const baseConfig = {
	pageName: '适老化改造申请管理',
	// 数据字典
	dictcodes: [],
	rowKey: 'id',
	tipLabel: 'name',
	addLabel: '新增',
	importLabel:'批量导入',
	api: {
		...api,
	},
	auth: {
		...api,
		mulitSet:'mulitSet',
	},
	paramsObj:{
		
	},
	noMulDel:true,// 只有单个删除，没有批量删除
	// 当前行操作按钮
	menuBtn: [
		{
			label: '详情',
			type: 'primary',
			operate: 'detail',
			auth: api.index,
			text: true,
			show: true,
		},
	
		{
			label: '编辑',
			type: 'primary',
			operate: 'edit',
			auth:api.edit,
			condition: "(row)=>(row.isEditDel)",
			text: true,
			show: true,
		},
		{
			label: '删除',
			type: 'danger',
			operate: 'delete',
			auth: api.delete,
			condition: "(row)=>(row.isEditDel)",
			text: true,
			show: true,
		}
	],
	importConfig :{
		tabSteps: [
			{
				label: '下载模板',
				download: '下载模板',
				active: 1,
				rules: [
					{
						label: '下载最新导入模板。共1个：',
						children: [
							{
								label: '《适老化改造申请信息表》',
								link: true,
								url: 'view_bscAdaptOld_apply_downloadTemplate',
							},
						],
					},
				],
			},
			{
				label: '导入《适老化改造申请信息表》',
				description: '',
				uploadTip: '适老化改造申请信息表',
				active: 2,
				url: 'view_bscAdaptOld_apply_import',
				code: 4006,
				rules: [
					{
						label: '导入规则说明:',
						children: [
							{
								label: '(1)导入模式为“覆盖导入”，待导入的适老化改造申请信息表会覆盖平台已存在的',
							},
						],
					},
				],
			},
		],
	},
	formConfig,
	dialogConfig: {
		width: '80%',
	},
	// 搜索
	searchConfig: {
		inline: true,
		showBtn: true,
		labelPosition: 'left',
		column: [
			{ label: '老人姓名', prop: 'name', type: 'input', span: 8 },
			{ label: '身份证号', prop: 'idCard', type: 'input', span: 8 },
			{
				label: '手机号码',
				prop: 'telephone',
				span: 8,
			},
			{
				label: '户籍区域',
				prop: 'cr_areas',
				type: 'AplidAreaSelect',
				span: 8,
			},
			{
				label: '现居区域',
				type: 'AplidAreaSelect',
				prop: 'areas',
				span: 8,
			},
			{
				label: '申请时间',
				prop: 'applicationTimeBetween',
				type: 'daterange',
				format: 'YYYY年MM月DD',
				valueFormat: 'YYYY-MM-DD',
				span: 8,
			},
			{
				label: '改造申请表上传情况',
				prop: 'isWithMaterial',
				type: 'select',
				dicData:isUploadDic,
				span: 8,
			},
		],
	},
	// 列表
	tableConfig,
	detailConfig:formConfig,
    saveFormate: async (form) => {
        let copyForm = cloneDeep(form)
		return new Promise((resolve, reject) => {
            let cr_areas_obj = getAreaObj(copyForm.cr_areas,'cr')
            let cr_areas_name_obj = getAreaObj(copyForm.cr_areas_name,'cr')
            let areas_obj = getAreaObj(copyForm.areas,'')
            let areas_name_obj = getAreaObj(copyForm.areas_name,'')

			resolve({
                ...copyForm,
                ...cr_areas_obj,
                ...areas_obj,
                ...cr_areas_name_obj,
                ...areas_name_obj,
                cr_areas:undefined,
                cr_areas_name:undefined,
                areas:undefined,
                areas_name:undefined
            })
		})
	},
	onLoadFormate: async (arr) => {
		let tableData = arr.map((item) => {
            // 处理区域
			if(item.applicationFile){
				item.isWithMaterial = '1'
			}else{
				item.isWithMaterial = '0'
			}
            // 处理编辑和删除控制
			let userInfo = getStore('userInfo')
			if(userInfo.classifyId==3){
				// 行政用户，用区域判断
				if(getBaseAreaData(item).myAreaId==getBaseAreaData(userInfo).myAreaId&&item.authOrganizationId==userInfo.organizationId){
					item.isEditDel = true
				}else{
					item.isEditDel = false
				}
			}else if(userInfo.classifyId==5){
				// 服务商
				if(item.authOrganizationId==userInfo.organizationId){
					item.isEditDel = true
				}else{
					item.isEditDel = false
				}
			}
			let cr_areas:any = getAreaObj(item,'cr','set')
            let areas:any = getAreaObj(item,'','set')
			item.cr_areas = cr_areas.areaIds
			item.cr_areas_name = cr_areas.names
			item.areas = areas.areaIds
			item.areas_name = areas.names
			return item
		})
		return Promise.resolve(tableData)
	},
	operateFormate: async (form, type) => {
		form.applicationFile1 = form.applicationFile
			let cr_areas:any = getAreaObj(form,'cr','set')
            let areas:any = getAreaObj(form,'','set')
			form.cr_areas = cr_areas.areaIds
			form.cr_areas_name = cr_areas.names
			form.areas = areas.areaIds
			form.areas_name = areas.names
		return Promise.resolve(form)
	},
	beforeOnLoadFormate(form) {
		let copyForm = cloneDeep(form)
		// 处理搜索条件
		if(copyForm.applicationTimeBetween){
			copyForm.applicationTimeBetween[0]=copyForm.applicationTimeBetween[0]+' 00:00:00'
			copyForm.applicationTimeBetween[1]=copyForm.applicationTimeBetween[1]+' 23:59:59'
			copyForm.applicationTimeBetween = copyForm.applicationTimeBetween.join(',')
		}
		let cr_areas_obj = getAreaObj(copyForm.cr_areas,'cr')
        let cr_areas_name_obj = getAreaObj(copyForm.cr_areas_name,'cr')
        let areas_obj = getAreaObj(copyForm.areas,'')
        let areas_name_obj = getAreaObj(copyForm.areas_name,'')
		
		return new Promise((resolve, reject) => {
			resolve({
                ...copyForm,
                ...cr_areas_obj,
                ...areas_obj,
                ...cr_areas_name_obj,
                ...areas_name_obj,
                cr_areas:undefined,
                cr_areas_name:undefined,
                areas:undefined,
                areas_name:undefined
            })
		})
	},
}
